package cn.wolfcode.common.web.interceptor;

import cn.wolfcode.common.constants.CommonConstants;
import cn.wolfcode.common.domain.UserInfo;
import cn.wolfcode.common.web.CommonCodeMsg;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.common.web.anno.RequireLogin;
import cn.wolfcode.redis.CommonRedisKey;
import com.alibaba.fastjson.JSON;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class RequireLoginInterceptor implements HandlerInterceptor {
    private StringRedisTemplate redisTemplate;
    public RequireLoginInterceptor(StringRedisTemplate redisTemplate){
        this.redisTemplate = redisTemplate;
    }
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        /**
//         * 传统项目需要跨域放行
//         */
//
//        if(handler instanceof HandlerMethod){
//            HandlerMethod handlerMethod = (HandlerMethod) handler;
//            String feignRequest = request.getHeader(CommonConstants.FEIGN_REQUEST_KEY);
//            if(!StringUtils.isEmpty(feignRequest) && CommonConstants.FEIGN_REQUEST_FALSE.equals(feignRequest) && handlerMethod.getMethodAnnotation(RequireLogin.class)!=null){
//                response.setContentType("application/json;charset=utf-8");
//                String token = request.getHeader(CommonConstants.TOKEN_NAME);
//                if(StringUtils.isEmpty(token)){
//                    response.getWriter().write(JSON.toJSONString(Result.error(CommonCodeMsg.TOKEN_INVALID)));
//                    return false;
//                }
//                UserInfo userInfo = JSON.parseObject(redisTemplate.opsForValue().get(CommonRedisKey.USER_TOKEN.getRealKey(token)),UserInfo.class);
//                if(userInfo==null){
//                    response.getWriter().write(JSON.toJSONString(Result.error(CommonCodeMsg.TOKEN_INVALID)));
//                    return false;
//                }
//                String ip = request.getHeader(CommonConstants.REAL_IP);
//                if(!userInfo.getLoginIp().equals(ip)){
//                    response.getWriter().write(JSON.toJSONString(Result.error(CommonCodeMsg.LOGIN_IP_CHANGE)));
//                    return false;
//                }
//            }
//        }
//        return true;
//    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1 跨域, 静态资源
        if(!(handler instanceof HandlerMethod)) {
            return true;
        }
        // 2 是controller中的方法
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        //获取请求来源数据
        String feignRequest = request.getHeader(CommonConstants.FEIGN_REQUEST_KEY);

        if(StringUtils.isEmpty(feignRequest) //mq调用
                || CommonConstants.FEIGN_REQUEST_TRUE.equals(feignRequest) //feign调用
                || handlerMethod.getMethodAnnotation(RequireLogin.class)==null){ //方法上没有@RequireLogin注解
            return true;
        }
        //请求从前端过来
        response.setContentType("application/json;charset=utf-8"); //响应成json数据
        String token = request.getHeader(CommonConstants.TOKEN_NAME);//获取前端传递过来的token
        //token null, 30m之内,30m之外
        // 异常一  没有登录,获取上一次登录错误
        if(StringUtils.isEmpty(token)){//token null
            response.getWriter().write(JSON.toJSONString(Result.error(CommonCodeMsg.TOKEN_INVALID)));
            return false;
        }

        UserInfo userInfo = JSON.parseObject(redisTemplate.opsForValue().get(CommonRedisKey.USER_TOKEN.getRealKey(token)),UserInfo.class);
        // 异常二: 上一次访问超过了30m
        if(userInfo==null){
            response.getWriter().write(JSON.toJSONString(Result.error(CommonCodeMsg.TOKEN_INVALID)));
            return false;
        }
        // 异常三
        String ip = request.getHeader(CommonConstants.REAL_IP);
        //当前前端的ip地址是否是登录时的ip地址
        if(!userInfo.getLoginIp().equals(ip)){
            response.getWriter().write(JSON.toJSONString(Result.error(CommonCodeMsg.LOGIN_IP_CHANGE)));
            return false;
        }
        return true;
    }
}


